package com.tianji.learning.controller;

import com.tianji.common.domain.dto.PageDTO;
import com.tianji.common.domain.query.PageQuery;
import com.tianji.common.utils.UserContext;
import com.tianji.learning.domain.dto.LearningPlanDTO;
import com.tianji.learning.domain.vo.LearningLessonVO;
import com.tianji.learning.domain.vo.LearningPlanPageVO;
import com.tianji.learning.service.ILearningLessonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 学生课表 前端控制器
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/lesson")
@Api(tags = "用户端-我的课表")
public class LearningLessonController {
    private final ILearningLessonService learningLessonService;

    @GetMapping("/page")
    @ApiOperation("分页查询我的课表")
    public PageDTO<LearningLessonVO> pageQuery(PageQuery query) {
        return learningLessonService.pageQuery(query);
    }

    @GetMapping("/now")
    @ApiOperation("查询正在学习的课程")
    public LearningLessonVO getNowLearningLesson() {
        return learningLessonService.getNowLearningLesson();
    }

    @DeleteMapping("/{courseId}")
    @ApiOperation("删除已失效课程")
    @ApiImplicitParam(name = "courseId", value = "失效课程id", required = true, dataTypeClass = Long.class)
    public void deleteLesson(@PathVariable("courseId") Long courseId) {
        //当前用户ID
        Long userId = UserContext.getUser();
        learningLessonService.deleteLesson(userId, courseId);
    }

    @GetMapping("/{courseId}/valid")
    @ApiOperation("检查课程是否有效")
    public Long isLessonValid(@PathVariable("courseId") Long courseId) {
        return learningLessonService.isLessonValid(courseId);
    }

    @GetMapping("/{courseId}")
    @ApiOperation("查询用户课表中指定课程状态")
    public LearningLessonVO getLessonStatusByCourseId(@PathVariable("courseId") Long courseId) {
        return learningLessonService.getLessonStatusByCourseId(courseId);
    }

    @GetMapping("/{courseId}/count")
    @ApiOperation("统计课程学习人数")
    public Integer countLearningLessonByCourse(@PathVariable("courseId") Long courseId) {
        return learningLessonService.countLearningLessonByCourse(courseId);
    }

    @PostMapping("/plans")
    @ApiOperation("创建学习计划")
    public void addPlans(@RequestBody LearningPlanDTO dto) {
        learningLessonService.addPlans(dto);
    }

    @GetMapping("/plans")
    @ApiOperation("查询学习计划进度")
    public LearningPlanPageVO getPlans(PageQuery pageQuery) {
        return learningLessonService.getPlans(pageQuery);
    }
}